package com.example.ppp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ppp.dto.UserDTO;
import com.example.ppp.entity.Address;
import com.example.ppp.entity.Count;
import com.example.ppp.entity.User;
import com.example.ppp.mapper.AddressMapper;
import com.example.ppp.mapper.CountMapper;
import com.example.ppp.service.UserService;
import com.example.ppp.mapper.UserMapper;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author admin
* @description 针对表【user】的数据库操作Service实现
* @createDate 2023-05-18 13:27:58
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Resource
    private UserMapper userMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private CountMapper countMapper;

    @Override
    public void insertUsers(){
        long start = System.currentTimeMillis();
        List<User> userList = new ArrayList<>();
        List<Count> countList = new ArrayList<>();
        User user;
        for(int i = 0 ;i < 50; i++) {
            user = new User();
            user.setName("name" + i);
            user.setAge(i+"");
            userList.add(user);
        }
        userMapper.insertBatchSomeColumn(userList);
        long end = System.currentTimeMillis();
        System.out.println("一万条数据总耗时：" + (end-start) + "ms" );
    }

    @Override
    public void insertUsers2(){
        long start = System.currentTimeMillis();
//        List<User> userList = new ArrayList<>();
        User user;
        for(int i = 0 ;i < 10000; i++) {
            user = new User();
            user.setName("name" + i);
            user.setAge(i+"");
//            userList.add(user);
            userMapper.insert(user);
        }
//        userMapper.insertBatchSomeColumn(userList);
        long end = System.currentTimeMillis();
        System.out.println("一万条数据总耗时：" + (end-start) + "ms" );
    }


    @Override
    public List<UserDTO> selectpage(UserDTO userDTO)
    {
        Page<User> userPage = new Page<>(1,50);
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StringUtils.checkValNotNull(userDTO.getName()),User::getName, userDTO.getName());
        if (userDTO.getAddressortelorage() != null)
        {
            LambdaQueryWrapper<Count> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
            addressLambdaQueryWrapper.eq(StringUtils.checkValNotNull(userDTO.getAddressortelorage()),Count::getTel,userDTO.getAddressortelorage());
            List<Count> countList = countMapper.selectList(addressLambdaQueryWrapper);
            ArrayList<Long> auids = new ArrayList<>();
            if (countList.size()>0)
            {
                for (Count count : countList) {
                    auids.add(count.getUserId());
                }
            }
            System.out.println("auids.size() = " + auids.size());
            wrapper.and(i-> i.in(auids.size()>0,User::getId,auids).or().eq(User::getAge, userDTO.getAddressortelorage()));
        }
       userMapper.selectPage(userPage,wrapper);
        List<User> records = userPage.getRecords();
        for (User record : records) {
            System.out.println("record = " + record);
        }
        return null;
    }
}




